package com.cwt_taxi_xa;

import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.cwt_taxi_xa.protocol.CardUtil;
import com.cwt_taxi_xa.protocol.DDUtil;
import com.cwt_taxi_xa.protocol.MCUUtil;
import com.cwt_taxi_xa.protocol.MeterEncoder;
import com.cwt_taxi_xa.protocol.voice.VoiceTool;
import com.cwt_taxi_xa.service.SerialPortService;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.Tools;

public class ActivitySPTest extends CWTBaseActivity{

	TextView tv_send, tv_rec;
	RadioGroup rg;
	Button bt1,bt3,bt4;

	public static boolean isView = false;
	public static int SPindex = 1;


	String str_send = "", str_rec = "";

	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.what==1001){
				byte[] data = (byte[]) msg.obj;
				if(str_rec.length()>800) str_rec = "";
				str_rec+=Tools.parseByte2HexStr2(data);
				//tv_rec.setText(str_rec);
				setRecText();
			}

			if(msg.what==1002){
				String str = (String) msg.obj;
				if(str_send.length()>800) str_send = "";
				str_send+=str;
				tv_send.setText(str_send);
			}

			if(msg.what == 1003){
				byte[] data = (byte[]) msg.obj;
				appendString(data);
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_aa_sptest);

		tv_send = (TextView) findViewById(R.id.tv_sp_send);
		tv_rec = (TextView) findViewById(R.id.tv_sp_rec);
		rg = (RadioGroup) findViewById(R.id.radioGroup1);
		bt1 = (Button) findViewById(R.id.bt1);
		bt3 = (Button) findViewById(R.id.bt3);
		bt4 = (Button) findViewById(R.id.bt4);

		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {
				switch (arg1) {
					case R.id.radio0:
						SPindex = 1;
						break;
					case R.id.radio1:
						SPindex = 2;
						break;
					case R.id.radio2:
						SPindex = 6;
						break;
					case R.id.radio3:
						SPindex = 7;
						break;
					case R.id.radio4:
						SPindex = 8;
						break;
					case R.id.radio5:
						SPindex = 9;
						//dataSerialConfig();
						break;
				}
			}
		});

		bt1.setOnClickListener(onclick);
		bt3.setOnClickListener(onclick);
		bt4.setOnClickListener(onclick);
	}

	private void dataSerialConfig(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				MCUUtil.setIO(2,true);
				MCUUtil.dataSerialConfig();
			}
		}).start();
	}

	OnClickListener onclick = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
				case R.id.bt1:
					sendData();
					break;

				case R.id.bt2:
					new Thread(new Runnable() {
						@Override
						public void run() {
						}
					}).start();
					break;

				case R.id.bt3:
					str_send = "";
					tv_send.setText(str_send);
					str_rec = "";
					tv_rec.setText(str_rec);
					break;

				case R.id.bt4:
					finish();
					break;

			}
		}
	};

	public byte[] testdata = Tools.parseHexStr2Byte("0123456789");
	public void sendData(){
		switch (SPindex) {
			case 1:
				testdata = DDUtil.getDDtimeData();
				SerialPortService.getInstance().sendSerialData1(testdata);
				break;
			case 2:

				//testdata = CardUtil.getTsm905Data(0x0000, Tools.parseHexStr2Byte("20 19 03 01 14 47 46"));
				//SerialPortService.getInstance().sendSerialData2(testdata);
				testdata = CardUtil.cardjy2(Tools.int2Bytes2(0x0020, 2));
				//SerialPortService.getInstance().sendSerialData2(testdata);
				//读卡
				CardUtil.mCardManager.changeTestState();
				CardUtil.mCardManager.startAndRead();
				break;
			case 6:
				testdata = MeterEncoder.queryMeterState();
				SerialPortService.getInstance().sendSerialData6(testdata);
				break;
			case 7:
				testdata = MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 47 0002"));
				SerialPortService.getInstance().sendSerialData7(testdata);
				break;
			case 8:
				testdata = MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 6B 000F 01 FC 04 00 00 FF FF FF FF 04 FF FF FF"));
				SerialPortService.getInstance().sendSerialData7(testdata);
				//SerialPortService.getInstance().sendSerialData7(testdata);
				//SerialPortService.getInstance().sendSerialData7(testdata);
				break;
			case 9:
				testdata = MCUUtil.getMCUData(MCUUtil.touchuan(VoiceTool.voiceEncode(0,new byte[]{})));
				SerialPortService.getInstance().sendSerialData7(testdata);
				break;
		}
		handler.sendMessage(handler.obtainMessage(1002, Tools.parseByte2HexStr(testdata)));
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		isView = true;
		switch (SPindex) {
			case 1:
				rg.check(R.id.radio0);
				break;
			case 2:
				rg.check(R.id.radio1);
				break;
			case 6:
				rg.check(R.id.radio2);
				break;
			case 7:
				rg.check(R.id.radio3);
				break;
			case 8:
				rg.check(R.id.radio4);
				break;
			case 9:
				rg.check(R.id.radio5);
				break;
		}

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		isView = false;
	}



	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		CardUtil.mCardManager.stop();
	}


	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		if(type==1017){//通用测试(can,声耕,串口)
			handler.sendMessage(handler.obtainMessage(1001, obj));
		}else if(type==1018){//卡测试
			handler.sendMessage(handler.obtainMessage(1003, obj));
		}
	}

	//追加文字描述
	private void appendString(byte[] buffer){
		if(buffer != null){
			try {
				String hexStr = Tools.parseByte2HexStr2(buffer);
				AppLog.d("ActivitySPTest", "appendString hexStr:" + hexStr);
				String result = Tools.parseByte2HexStr(Tools.byteTobyte(buffer, buffer.length-5, 2));
				String mid = Tools.parseByte2HexStr(Tools.byteTobyte(buffer, 6, 2));
				String str = "";
				if(result.equals("9000")){
					str = getColorText("读卡成功!","#00FF00");
					//str = "读卡成功!";
				}else if(result.equals("FF0F")){
					//"未检测到卡片"
					str = getColorText("未检测到卡片!","#FF0000");
					//str = "未检测到卡片!";
				}else if(result.equals("6A88")){
					str = getColorText("秘钥不匹配!","#FF0000");
					//str = "秘钥不匹配!";
					//"秘钥不匹配"
				}else if(result.equals("6A83")){
					str = getColorText("指令出错!","#FF0000");
					//str = "指令出错!";
					//指令出错
					//按目前流程，正常情况下发生在读不到更多车牌记录
					//"车牌不匹配"
				}else{
					if(mid.equals("0020")||mid.equals("0021")){
						//"读卡失败"
						str = getColorText("读卡失败!","#FF0000");
						//str = "读卡失败!";
					}
				}
				str_rec += str;
				setRecText();
				AppLog.d("ActivitySPTest", "appendString result:" + result + " mid:" + mid + " str:" + str );


			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}


		//tv_rec.append(text);
		//;
		//tv.setTextSize(18);
		//tv.setText(Html.fromHtml(str));
	}

	//	#FF0000 红色
	//	#00FF00	绿色
	private String getColorText(String text,String color){
		return "<font color='"+ color +"'>" + text +"</font>";
	}

	private synchronized void setRecText(){
		if(tv_rec != null){
			tv_rec.setText(Html.fromHtml(str_rec));
		}
	}
}
